package com.lxyk.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxyk.yygh.cmn.client.DictFeign;
import com.lxyk.yygh.enums.DictEnum;
import com.lxyk.yygh.model.user.Patient;
import com.lxyk.yygh.user.mapper.PatientMapper;
import com.lxyk.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

//预约下单需要选择就诊人，因此我们要实现就诊人管理，前端就诊人管理其实就是要实现一个完整的增删改查
@Service
public class PatientServiceImpl extends
        ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private PatientMapper patientMapper;
    
    @Autowired
    private DictFeign dictFeign;
    
    
    //获取就诊人列表
    //userId 对应的是userInfo的下单的人的id
    //一个userId可能挂了很多次号
    @Override
    public List<Patient> findAllUserId(Long userId) {

        LambdaQueryWrapper<Patient> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Patient::getUserId, userId);
        List<Patient> patientList = patientMapper.selectList(lambdaQueryWrapper);

//        进行处理，进行数据封装
        patientList.stream().forEach(patient -> {
            //封装数据字典信息
           this.packPatient(patient);
        });
        return patientList;
    }


    //Patient对象里面其他参数封装
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString =
                dictFeign.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型
        String contactsCertificatesTypeString =
                dictFeign.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeign.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeign.getName(patient.getCityCode());
        //区
        String districtString = dictFeign.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }


    //根据id获取就诊人信息
    //这是病人的id
    @Override
    public Patient getPatientById(Long id) {
        Patient patient = patientMapper.selectById(id);
        Patient packPatient = this.packPatient(patient);
        return packPatient;
    }
}